Welcome back.
One module that we can script real quick in this lecture is going to be our lightning service.
All the service is going to do is continually loop and randomly fire an event to clients to summon lightning.
So that means the only thing that we're going to need in here is replicated storage.
To make a reference to the game comms event, we can quickly create our table.
That will represent our service, and we'll make sure to return it at the end of our script here.
And then we can make a reference to our game comms event.
That remotes game communication.
And then we can also get our game comms enum.
So we'll require from upgraded storage dot modules, dot enums, dot game communication enum.
And then we're also going to need a random data type.
Then we can just have a start function inside of our lightning service.
And all this is going to do is it's going to spawn a while loop in a new thread so we can use task dot
defer because we don't need it to run instantaneously.
And we'll just have a loop that runs forever.
And all we're going to do is we're going to use our RNG next integer function, pass a number of like
something between 1 and 35.
And if that's equal to one, then we can use the game comms event and fire to all of the clients in
our game to summon lightning.
So game comms enum dot two client dot lightning.
And then just to make sure that lightning doesn't fire too often after we fire to all the clients,
we'll just wait something like three seconds.
And then this whole while loop itself will run every single second.
So we'll have a yield for one second, and then it'll do the check.
And then that means lightning should fire maybe once about every 35 seconds.
And that's all we need to do for this service.
Now to actually see lightning inside of our game.
Let's go ahead and reset the brightness back to be zero.
And let's go ahead and enable fog to its original state of 135.
And let's go ahead and test out our lightning.
So at some point we should have lightning.
There we go.
And there's our lightning.
So we further added to the ambiance in our game where the brightness in the workspace increases and
we get some nice lightning sound effects.
And because we're looping it from the server, that means all of the clients in our game will have lightning
effects applied at the exact same time as each other.
Let's see if we can get another lightning strike here.
And there we go.
So looks like our ambience is coming together.
Great.
That's all for me in this lecture, and I will see you in the next one.